Source File
xfn.go
Belonging Package
github.com/ChrisTrenkamp/goxpath/tree
package tree
import (
)
//Ctx represents the current context position, size, node, and the current filtered result
type Ctx struct {
NodeSet
Pos int
Size int
}
//Fn is a XPath function, written in Go
type Fn func(c Ctx, args ...Result) (Result, error)
//LastArgOpt sets whether the last argument in a function is optional, variadic, or neither
type LastArgOpt int
//LastArgOpt options
const (
None LastArgOpt = iota
Optional
Variadic
)
//Wrap interfaces XPath function calls with Go
type Wrap struct {
Fn Fn
//NArgs represents the number of arguments to the XPath function. -1 represents a single optional argument
NArgs int
LastArgOpt LastArgOpt
}
//Call checks the arguments and calls Fn if they are valid
func ( Wrap) ( Ctx, ...Result) (Result, error) {
switch .LastArgOpt {
case Optional:
if len() == .NArgs || len() == .NArgs-1 {
return .Fn(, ...)
}
case Variadic:
if len() >= .NArgs-1 {
return .Fn(, ...)
}
default:
if len() == .NArgs {
return .Fn(, ...)
}
}
return nil, fmt.Errorf("Invalid number of arguments")
}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |